package com.hyper_yang.algorithmRecord.June;

import java.util.Arrays;

// 2025.06.22 将字符串拆分为若干长度为 k的组
public class LeetCode_2138 {
    public String[] divideString(String s, int k, char fill) {
        int groupNum = s.length() / k;
        int other = s.length() % k;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < groupNum; i++) {
            String str = s.substring(i * k, (i + 1) * k); // 迭代
            sb.append(str).append(" ");
        }
        if (other != 0) {
            sb.append(s.substring(groupNum * k)); // 最后一组
            for (int i = 0; i < (k - other); i++) { // 补充字符
                sb.append(fill);
            }
        }
        return sb.toString().split(" ");
    }

    public static void main(String[] args) {
        LeetCode_2138 solution = new LeetCode_2138();
        System.out.println(Arrays.toString(solution.divideString("abcdefghi", 3, 'x'))); // ["abc","def","ghi"]
        System.out.println(Arrays.toString(solution.divideString("abcdefghij", 3, 'x'))); // ["abc","def","ghi","jxx"]

    }
}
